package arcane.object.game.equipment;

import java.awt.Graphics2D;

import arcane.object.ImagePath;
import arcane.object.game.sprite.Actor;

public class Equipment {
	/**
	 * Magic bonus
	 */
	protected int magic;

	/**
	 * Defense bonus
	 */
	protected int defense;

	/**
	 * Magic defense bonus
	 */
	protected int magicDefense;

	/**
	 * Attack bonus
	 */
	protected int attack;

	/**
	 * Speed bonus
	 */
	protected int speed;

	/**
	 * Cast speed bonus
	 */
	protected int castSpeed;

	/**
	 * Layer this equipment is rendered in. Equipment in higher layers will be
	 * rendered before equipment in lower layers.
	 */
	protected int layer;

	/**
	 * Description for this piece of equipment.
	 */
	private String description;

	// Name of this piece of equipment
	private String name;
	/*
	 * Equip script for this equipment. Should contain methods 'equip(Actor)'
	 * and 'unequip(Actor)' Method equip will be called when actor equips item.
	 * Method unequip will be called when actor equips item.
	 */
	private String equipScript;

	protected ImagePath iconImg;
	protected ImagePath overlayImg;

	/**
	 * PvP point cost is cost pvp points this item requires to unlock.
	 */
	protected int pvpCost;

	public Equipment() {
		this("untitled");
	}

	public Equipment(String name) {
		this.name = name;
	}

	public int getPVPCost() {
		return pvpCost;
	}

	public void setPVPCost(int pvpCost) {
		this.pvpCost = pvpCost;
	}

	/**
	 * Sets the description of this equipment.
	 * 
	 * @param description
	 *            this equipment's description
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * Returns the description of this equipment.
	 * 
	 * @return this equipment's description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * Sets this equipment's magical defense
	 * 
	 * @param magDef
	 *            this equipment's new magical defense
	 */
	public void setMagicDefense(int magDef) {
		this.magicDefense = magDef;
	}

	/**
	 * Returns this equipment's magic defense
	 * 
	 * @return this equipment's magic defense
	 */
	public int getMagicDefense() {
		return magicDefense;
	}

	/**
	 * Sets this equipment's magic
	 * 
	 * @param magic
	 *            this equipment's magic
	 */
	public void setMagic(int magic) {
		this.magic = magic;
	}

	/**
	 * Returns this equipment's magic
	 * 
	 * @return equipment's magic
	 */
	public int getMagic() {
		return magic;
	}

	/**
	 * Sets this equipment's attack
	 * 
	 * @param atk
	 *            equipment's attack
	 */
	public void setAttack(int atk) {
		attack = atk;
	}

	/**
	 * Returns this equipment's attack
	 * 
	 * @return this equipment's attack
	 */
	public int getAttack() {
		return attack;
	}

	/**
	 * Sets this equipment's physical defense
	 * 
	 * @param def
	 *            equipment's physical defense
	 */
	public void setDefense(int def) {
		defense = def;
	}

	/**
	 * Returns this equipment's physical defense
	 * 
	 * @return equipment's physical defense
	 */
	public int getDefense() {
		return defense;
	}

	/**
	 * Sets this equipment's speed
	 * 
	 * @param speed
	 *            equipment's speed
	 */
	public void setSpeed(int speed) {
		this.speed = speed;
	}

	/**
	 * Return's this equipment's speed
	 * 
	 * @return equipment's speed
	 */
	public int getSpeed() {
		return speed;
	}

	/**
	 * Sets this equipment's cast speed
	 * 
	 * @param castSpeed
	 *            equipment's cast speed
	 */
	public void setCastSpeed(int castSpeed) {
		this.castSpeed = castSpeed;
	}

	/**
	 * Returns equipment's cast speed
	 * 
	 * @return this equipment's cast speed
	 */
	public int getCastSpeed() {
		return castSpeed;
	}

	/**
	 * Sets this equipment's layer. Equipment in higher layers are rendered over
	 * equipment in lower layers.
	 * 
	 * @param layer
	 *            this equipment's new layer
	 */
	public void setLayer(int layer) {
		this.layer = layer;
	}

	/**
	 * Returns the layer this equipment is rendered in. Equipment in higher
	 * layers are rendered over equipment in lower layers.
	 * 
	 * @return this equipments layer
	 */
	public int getLayer() {
		return layer;
	}

	/**
	 * Sets this equipment's name
	 * 
	 * @param name
	 *            equipment's new name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Returns this equipment's name
	 * 
	 * @return this equipment's name
	 */
	public String getName() {
		return name;
	}

	/**
	 * Sets this equipment's script
	 * 
	 * @param script
	 *            this equipment's new script
	 */
	public void setScript(String script) {
		this.equipScript = script;
	}

	/**
	 * Returns this equipment's script
	 * 
	 * @return this equipment's script
	 */
	public String getScript() {
		return equipScript;
	}

	/**
	 * Equips this equipment to an actor by adding this equipment's attributes
	 * to his attributes.
	 * 
	 * @param a
	 *            specified actor
	 */
	public void equip(Actor a) {
		a.setAttack(a.getAttack() + attack);
		a.setDefense(a.getDefense() + defense);
		a.setSpeed(a.getSpeed() + speed);
		a.setMagicAttack(a.getMagicAttack() + magic);
		a.setMagicDefense(a.getMagicDefense() + magicDefense);
		a.setCastSpeed(a.getCastSpeed() + castSpeed);
	}

	/**
	 * Unequips this equipment from an actor by subtracting this equipment's
	 * attributes from his attributes.
	 * 
	 * @param a
	 *            specified actor
	 */
	public void unequip(Actor a) {
		a.setAttack(a.getAttack() - attack);
		a.setDefense(a.getDefense() - defense);
		a.setSpeed(a.getSpeed() - speed);
		a.setMagicAttack(a.getMagicAttack() - magic);
		a.setMagicDefense(a.getMagicDefense() - magicDefense);
		a.setCastSpeed(a.getCastSpeed() - castSpeed);
	}

	/**
	 * Render's this equipment's icon with a specified graphics context at a
	 * specified location.
	 * 
	 * @param g
	 *            graphics context
	 * @param x
	 *            x location to render icon
	 * @param y
	 *            y location to render icon
	 */
	public void renderIcon(Graphics2D g, int x, int y) {
		if (iconImg.getImage() != null)
			g.drawImage(iconImg.getImage(), x, y, null);
	}

	/**
	 * Paints this equipment's overlay
	 * 
	 * @param g
	 */
	public void renderOverlay(Graphics2D g) {
		if (overlayImg.getImage() != null) {
			g.drawImage(overlayImg.getImage(), 0, 0, null);
		}
	}
}